@Echo Off
echo =====+=====+=====+=====+=====+=====+
echo Batch File Versioner - creates a uniquely-named copy or archive.
echo Version 1.1 -- Marko Zarkovic 2006-2009
echo Note: If no "zip" program is installed, the file(s) will be copied but not compressed.
echo =====+=====+=====+=====+=====+=====+
set sTime=%time%
echo Start time %sTime%
FOR /F "Tokens=1-5 delims=:. " %%I in ( " %time% " ) Do set unqtime=%%I%%J-%%K%%L
echo Unique stamp: %unqtime%
echo =====+=====+=====+=====+=====+=====+
echo Working in directory... %CD%
if "%*"=="" goto BADPARAMS
echo Parameter is... %*
Set OrigInput=%*
Set Input=%*
Set /a CNT=0
Set STR=
Set Captured=

:: Capture name of file
:loop1
SET /a CNT=%CNT%+1
::For /F "tokens=1* delims=\" %%a in ("%Input%") do echo %%a
For /F "tokens=1* delims=\" %%a in ("%Input%") Do Set Input=%%b
If NOT "%INPUT%"=="" set Captured=%INPUT%
If "%Input%"=="" GoTo ENDLOOP1
GOTO LOOP1
:ENDLOOP1
echo =====+=====+=====+=====+=====+=====+
echo Segment count: %CNT%
echo Source name: %CAPTURED%
echo =====+=====+=====+=====+=====+=====+
REM Slight difference between W2K and WXP
set tfile1= %TEMP%\T1%unqtime%.DAT
ver > %tfile1%
find "Windows 2000" < %tfile1% > nul
if not %errorlevel%==1 goto OSW2K

find "Windows XP" < %tfile1% > nul
if not %errorlevel%==1 goto OSWXP
goto OSUNKNOWN

:OSW2K
echo Detected OS W2K
FOR /F "tokens=1-5 delims=/ " %%I IN ('DATE /t') DO SET mydate=y%%Lm%%Kd%%J
goto SETTIME

:OSUNKNOWN
echo OS unknown. Using default behaviour. (WXP)

:OSWXP
echo Detected OS WXP
FOR /F "tokens=1-4 delims=/ " %%I IN ('DATE /t') DO SET mydate=y%%Km%%Jd%%I
goto SETTIME
echo =====+=====+=====+=====+=====+=====+
:SETTIME
SET mytime=%unqtime%

:SETNAME
if  %CNT%==1 ( 
	set prefix=
) else set prefix =%CD%\

set MYARCH="%prefix%%MYDATE%-%MYTIME%_%CAPTURED%"
if exist %MYARCH% goto FILEEXISTS
echo Generated the filename %MYARCH%

echo =====+=====+=====+=====+=====+=====+
::Is Dir or file?
set tfile2= %TEMP%\T2%unqtime%.DAT
set ISDIR=0
dir /b /ad "%cd%" > %tfile2%
find "%CAPTURED%" < %tfile2% > nul
if %errorlevel%==0 set ISDIR=1
if "%cd%"=="%OrigInput%" set ISDIR=1
echo Dir check: %ISDIR% in %cd%
echo =====+=====+=====+=====+=====+=====+

echo Checking for ZIP...
set CANZIP=0
zip >nul
if not %ERRORLEVEL%==0 (
	set CANZIP=0
) else if %ERRORLEVEL%==0 ( set CANZIP=1 )

if %CANZIP%==1 (
	echo Zip is installed.
) else echo No Zip programme found. 

if %CANZIP%==1 (
 goto DOZIP
) else goto DOCOPY

:DOZIP
echo Running Zip...
if %ISDIR%==1 (
 zip -9r %MYARCH%.zip "%OrigInput%"
) else zip -9jr %MYARCH%.zip "%OrigInput%"
goto FINALREPORT

:DOCOPY
echo Doing Copy...
if %ISDIR%==1 ( 
	xcopy /i /q /s "%OrigInput%" %MYARCH%
	)  else copy /v /y "%OrigInput%" %MYARCH%
goto FINALREPORT

echo =====+=====+=====+=====+=====+=====+
:FINALREPORT
echo Versioned %OrigInput% to %MYARCH%
echo =====+=====+=====+=====+=====+=====+
echo Clean-up...
del %tfile1% > nul
del %tfile2% > nul
echo =====+=====+=====+=====+=====+=====+
echo Started %sTime%
echo Ended   %time%

goto END
:FILEEXISTS
echo The file name exists. Wait and try again.

goto END
:BADPARAMS
echo Missing a parameter.

goto END
:CMDError

:END
echo ... Done.

